<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include($root . "/util/session.php"); //checks that the user is logged in
include($root . "/util/privilege_check.php");
checkPrivilege("teacher");

if (isset($_SESSION['chapterPanel']) && isset($_SESSION['subjectPanel'])) {
    $chapter = $_SESSION['chapterPanel'];
    $subject = $_SESSION['subjectPanel'];

    $sql1 = "DELETE FROM results WHERE subjectID = '$subject' AND chapterID = '$chapter'";
    $sql2 = "DELETE FROM results_info WHERE subjectID = '$subject' AND chapterID = '$chapter'";
    $sql3 = "DELETE FROM tests WHERE subjectID = '$subject' AND chapterID = '$chapter'";

    if (mysqli_query($db, $sql1)) {
        if (mysqli_query($db, $sql2)) {
            if (mysqli_query($db, $sql3)) {
                mysqli_close($db);
                header("Location: ../teacher.php");
            } else {
                echo "Error: " . $sql3 . "<br>" . mysqli_error($db);
            }
        } else {
            echo "Error: " . $sql2 . "<br>" . mysqli_error($db);
        }
    } else {
        echo "Error: " . $sql1 . "<br>" . mysqli_error($db);
    }
}
